package xyz.zhouzhaodong.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.zhouzhaodong.crm.dto.ContractDto;
import xyz.zhouzhaodong.crm.entity.Contract;
import xyz.zhouzhaodong.crm.entity.Customer;
import xyz.zhouzhaodong.crm.mapper.ContractMapper;
import xyz.zhouzhaodong.crm.mapper.CustomerMapper;
import xyz.zhouzhaodong.crm.service.IContractService;
import xyz.zhouzhaodong.crm.util.RestMessage;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 合同 服务实现类
 * </p>
 *
 * @author zhouzhaodong
 * @since 2020-06-18
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService {

    @Resource
    ContractMapper contractMapper;

    @Resource
    CustomerMapper customerMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage addContract(ContractDto contractDto) {
        try {
            Contract contract = contractDtoToContract(contractDto);
            String id = UUID.randomUUID().toString().replaceAll("-", "");
            contract.setId(id);
            contract.setCreateUser(contractDto.getStaffId());
            contract.setCreateTime(new Date());
            contractMapper.insert(contract);
            return RestMessage.success(id);
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage deleteContract(ContractDto contractDto) {
        try {
            Contract contract = new Contract();
            contract.setId(contractDto.getId());
            contract.setFlag(1);
            contract.setUpdateUser(contractDto.getStaffId());
            contract.setUpdateTime(new Date());
            contractMapper.updateById(contract);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestMessage updateContract(ContractDto contractDto) {
        try {
            Contract contract = contractDtoToContract(contractDto);
            contract.setUpdateUser(contractDto.getStaffId());
            contract.setUpdateTime(new Date());
            contractMapper.updateById(contract);
            return RestMessage.success();
        } catch (Exception e) {
            return RestMessage.failed(e.getMessage());
        }
    }

    @Override
    public RestMessage selectContract(ContractDto contractDto) {
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("customer_id", contractDto.getCustomerId());
        if(contractDto.getId() != null && !"".equals(contractDto.getId())){
            queryWrapper.eq("id", contractDto.getId());
        }
        if(contractDto.getFlag() != null){
            queryWrapper.eq("flag", contractDto.getFlag());
        } else {
            queryWrapper.eq("flag", 0);
        }
        List<Contract> list = contractMapper.selectList(queryWrapper);
        return RestMessage.success(list);
    }

    @Override
    public RestMessage selectContractList(ContractDto contractDto) {
        // 查询合同信息，放入用户信息
        Integer flag = contractDto.getFlag();
        if(flag == null){
            contractDto.setFlag(0);
        }
        Page<ContractDto> page = new Page<>();
        page.setCurrent(contractDto.getPageNo());
        page.setSize(contractDto.getPageSize());
        IPage<ContractDto> list = contractMapper.selectContractList(contractDto, page);
        for(ContractDto c : list.getRecords()){
            // 组合金额和货币
            String currency = c.getCurrency();
            if (currency != null && !"".equals(currency)){
                String contractMoney = c.getContractMoney();
                c.setContractMoney(contractMoney + " (" + currency + ")");
            }
            Customer customer = customerMapper.selectById(c.getCustomerId());
            c.setCustomer(customer);
        }
        return RestMessage.success(list);
    }

    /**
     * 实体类转化
     *
     * @param contractDto
     * @return
     */
    private Contract contractDtoToContract(ContractDto contractDto) {

        Contract contract = new Contract();
        contract.setId(contractDto.getId());
        contract.setCustomerId(contractDto.getCustomerId());
        contract.setContractTime(contractDto.getContractTime());
        contract.setContractMoney(contractDto.getContractMoney());
        contract.setCurrency(contractDto.getCurrency());
        contract.setFormPayment(contractDto.getFormPayment());
        contract.setStatus(contractDto.getStatus());
        contract.setRemark(contractDto.getRemark());
        contract.setFlag(contractDto.getFlag());
        return contract;

    }

}
